﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using IchiaTester.Utils;

namespace IchiaTester
{
    /// <summary>
    /// 编辑/新增用户窗体
    /// </summary>
    public partial class UserEditForm : Form
    {
        private UserForm userform;
        private int userCount = 0;
        public int selectUser = -1; //-1 is not select
        public bool isAdd = false; // 控制是新增还是修改用户

        public UserEditForm(UserForm form)
        {
            this.userform = form;
            InitializeComponent();
        }

        /// <summary>
        /// 编辑用户窗体创建前触发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserEditForm_Load(object sender, EventArgs e)
        {
            //Get select index 获取被选中的用户的索引 -1代表未选中任何用户
            selectUser = userform.GetSelectIndex();

            //取得user list count
            if (userform.userlistView.Items.Count != 0)
            {
                userCount = userform.userlistView.Items.Count;
            }

            //can not Modify user name 
            if (!isAdd)
            {
                // 修改操作无法编辑用户名,新增操作则可以编辑
                tbUserName.Enabled = false;
            }

            /*
             * 登入用户是超级用户,则可以设置这三种角色 User Engineer Admin
             */
            if (SystemSetting.isAdmin)
            {
                cbPermission.Items.Add("User");
                cbPermission.Items.Add("Engineer");
                cbPermission.Items.Add("Admin");
            }

            /*
             * 登入用户是工程师,则可以设置这两种角色 User Engineer
             */
            if (SystemSetting.isEngineer)
            {
                cbPermission.Items.Add("User");
                cbPermission.Items.Add("Engineer");
            }
        }

        /// <summary>
        /// 点击"取消"按钮,触发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            // 关闭当前窗体
            this.Close();
        }

        /// <summary>
        /// 点击"确定"按钮,触发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (tbFactory.Text != "" && tbUserName.Text != "" && tbPassword.Text != "" && cbPermission.Text != "")
            {
                // 检查用户名
                bool isNumeric = Regex.IsMatch(tbUserName.Text, @"^\d+$");
                if (isNumeric)
                {
                    // 是数字
                    MessageBox.Show("用戶名稱不可以全部都是數字", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                // 新增用户
                if (isAdd)
                {
                    // 保存到listview
                    ListViewItem item = new ListViewItem(userCount.ToString());
                    item.SubItems.Add(tbFactory.Text);
                    item.SubItems.Add(tbUserName.Text);
                    item.SubItems.Add(cbPermission.Text);
                    userform.userlistView.Items.Add(item);

                    //儲存到list
                    UserInfo userInfo = new UserInfo();
                    userInfo.FactoryName = tbFactory.Text;
                    userInfo.HashPassword = DataSecureHelper.GetHash(tbPassword.Text);
                    userInfo.Permission = cbPermission.Text;

                    if (SystemSetting.UserInfoDict == null)
                    {
                        SystemSetting.UserInfoDict = new Dictionary<string, UserInfo>();
                    }

                    SystemSetting.UserInfoDict.Add(tbUserName.Text, userInfo);
                }
                else // 修改用户
                {
                    //Update listview
                    ListViewItem selectedItem = userform.userlistView.SelectedItems[0];
                    string oldName = selectedItem.SubItems[2].Text;

                    // Assuming the ListView has four columns
                    //string column1 = selectedItem.SubItems[0].Text;
                    selectedItem.SubItems[1].Text = tbFactory.Text;
                    selectedItem.SubItems[2].Text = tbUserName.Text;
                    selectedItem.SubItems[3].Text = cbPermission.Text;

                    //Delete old user info
                    if (SystemSetting.UserInfoDict.ContainsKey(oldName))
                    {
                        SystemSetting.UserInfoDict.Remove(oldName);
                    }

                    //Add new user info
                    UserInfo userInfo = new UserInfo();
                    userInfo.FactoryName = tbFactory.Text;
                    userInfo.HashPassword = DataSecureHelper.GetHash(tbPassword.Text);
                    userInfo.Permission = cbPermission.Text;

                    SystemSetting.UserInfoDict.Add(tbUserName.Text, userInfo);
                }

                this.Close();
            }
            else
            {
                MessageBox.Show("欄位不能空白", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}